<template>
  <!--我的资源-->
  <div class="box">
    <avue-crud ref="crud"
               v-model="form"
               :page="page"
               :data="tableData"
               :table-loading="tableLoading"
               :option="tableOption"
               @on-load="getList"
               @search-change="searchChange"
               @refresh-change="refreshChange"
               @size-change="sizeChange"
               @current-change="currentChange"
               @row-update="handleUpdate"
               @row-save="handleSave"
               @row-del="rowDel">
      <template slot="menu" slot-scope="scope">
        <el-button :size="scope.size" icon="el-icon-view" type="text" @click="view(scope.row)">查看</el-button>
      </template>
      <template slot-scope="scope" slot="fileForm">
        <el-upload
          class="upload-demo"
          :auto-upload="false"
          :limit="1"
          :on-change="handleChange"
          :on-remove="handleRemove"
          :file-list="form.file">
          <el-button size="small" type="primary">点击上传</el-button>
        </el-upload>
      </template>
    </avue-crud>
  </div>
</template>

<script>
    import * as u from '@/util/util'
    import {tableOption} from '@/js/bus/resource/myList.js'
    import tableMixins from "@/mixins/tableMixins.vue"

    export default {
        name: "myList",
        mixins: [tableMixins],
        data() {
            return {
                //访问接口前缀
                url: "bus-resource",
                //重写list接口
                listUrl:"findPageByMe",
                // 表格参数
                tableOption: tableOption,
                form: {
                    //文件集合
                    file: [],
                    //资源名
                    name: '',
                    //资源类型
                    type: '',
                    //是否共享
                    isShare: '',
                }
            }
        },
        methods: {
            //查看当前办件
            view(row) {
                window.open(u.getFileAllUrl("/"+row.path));
            },

            /**
             * 新增保存
             * @param row 行数据
             * @param done 完成回调
             * @param loading 加载回调
             */
            handleSave: function (row, done, loading) {
                //定义formData
                let formData = new FormData();
                formData.append("name", this.form.name);
                formData.append("type", this.form.type);
                formData.append("isShare", this.form.isShare);
                formData.append("file", this.form.file[0].raw);
                u.req({
                    url: "/" + this.url + "/" + this.addUrl,
                    method: "post",
                    data: formData,
                }).then((res) => {
                    //关闭弹框
                    this.refreshChange();
                    this.$message.success("操作成功！");
                    done();
                })
                loading();
            },


            /**
             *  文件删除回调
             **/
            handleRemove(file, fileList) {
                this.form.file = fileList;
            },

            /**
             *  文件改变回调
             **/
            handleChange(file, fileList) {
                this.form.file = fileList;
            },
        }
    }
</script>

<style scoped>

</style>
